package com.hpp.utils.email;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Date;
import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.hpp.utils.DateUtils;

@Service
public class SendEmail {
	
	@Value("${email_host:}")
	private String email_host;
	@Value("${email_username:}")
	private String email_username;
	@Value("${email_password:}")
	private String email_pwd;
	@Value("${email_temp:}")
	private Integer email_temp;
	
	/**
	 * 
	 * @param toEmail 接收邮箱
	 * @param title  标题
	 * @return
	 * @throws NoSuchAlgorithmException 
	 */
	public boolean send(HttpServletRequest request,String toEmail,String title) throws NoSuchAlgorithmException{
		MailOperation operation = new MailOperation();
		//邮箱内容
		StringBuffer sb = new StringBuffer();
		//密码学的随机数生成规则，其输出结果较难预测
		SecureRandom rand = SecureRandom.getInstance("SHA1PRNG");
		//验证码
		String code = String.valueOf((int)((rand.nextDouble()*9+1)*100000));
		//保存邮箱验证码
		//saveEmailCode(code, toEmail);
		//邮箱验证码存入redis中
		request.getSession().setAttribute(ResultCode.MAIL_CODE,code);
		request.getSession().setAttribute(ResultCode.MAIL,toEmail);
		//redis.setRedisUserInfo(request.getSession(), ResultCode.MAIL_CODE, code);
		//redis.setRedisUserInfo(request.getSession(), ResultCode.MAIL, toEmail);
		
		//String ip = SystemUtils.getIpAddr(request);
		sb.append("<p><span microsoft=\"\" yahei=\"\" font-size:=\"\" color:=\"\" background-color:=\"\" font-weight:=\"\" font-style:=\"\" text-decoration:=\"\" style=\"font-family: &#39;&#39;;\">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style=\"line-height: 1.5; background-color: window; font-size: 16px;\">如果非本人操作,请忽略!</span></p>");
		sb.append("<p><span style=\"font-family: &quot;&quot;; font-size: 16px;\"><br/></span></p>");	
		sb.append("<p><span style=\"font-family: &quot;&quot;; font-size: 16px;\">&nbsp; &nbsp; <span style=\"font-family: &quot;&quot;; font-size: 18px;\">您的验证码为</span>:</span></p>");
    
		sb.append("<p><span style=\"font-size: 16px;\"><span microsoft=\"\" yahei=\"\" font-size:=\"\" color:=\"\" background-color:=\"\" font-weight:=\"\" font-style:=\"\" text-decoration:=\"\" style=\"font-size: 16px; font-family: &quot;&quot;;\">&nbsp; &nbsp;&nbsp;</span><span style=\"font-family: &quot;&quot;; font-size: 10.5pt; line-height: 1.5; background-color: window;\">&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style=\"font-size: 16px; color: rgb(255, 0, 0);\"><span style=\"font-family: &quot;&quot;; font-size: 10.5pt; line-height: 1.5; background-color: window;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style=\"font-family: &quot;&quot;; line-height: 1.5; background-color: window; text-decoration: underline; font-size: 24px;\"><strong>"+code+"</strong></span></span></span></p>");
		sb.append("<p> <span style=\"font-size: 16px;\"><span microsoft=\"\" yahei=\"\" font-size:=\"\" color:=\"\" background-color:=\"\" font-weight:=\"\" font-style:=\"\" text-decoration:=\"\" style=\"font-size: 16px; font-family: &quot;&quot;;\">&nbsp; &nbsp; 温馨提示：</span><span style=\"font-family: &quot;&quot;; line-height: 1.5; background-color: window; font-size: 16px;\">&nbsp;验证码有效时间为五分钟，请您尽快进入进行验证！</span></span></p>");
//		sb.append("<p><span style=\"font-size: 16px;\"><span microsoft=\"\" yahei=\"\" font-size:=\"\" color:=\"\" background-color:=\"\" font-weight:=\"\" font-style:=\"\" text-decoration:=\"\" style=\"font-size: 16px; font-family: &quot;&quot;;\">&nbsp; &nbsp;</span><span style=\"font-family: &quot;&quot;; font-size: 10.5pt; line-height: 1.5; background-color: window;\">&nbsp;</span><span style=\"background-color: window; font-family: &quot;&quot;; font-size: 10.5pt; line-height: 1.5;\">北京电力交易中心统一门户网站：(测试地址)</span></span><span style=\"background-color: window; font-family: &quot;&quot;; line-height: 1.5; font-size: 16px; text-decoration: underline;\"><a>http://local.hpp-china.com:8009/bpxgatetwayweb/pages/default/index.html</a></span></p>");
		sb.append("<p><span style=\"font-size: 16px;\"><span microsoft=\"\" yahei=\"\" font-size:=\"\" color:=\"\" background-color:=\"\" font-weight:=\"\" font-style:=\"\" text-decoration:=\"\" style=\"font-size: 16px; font-family: &quot;&quot;;\">&nbsp; &nbsp;&nbsp;</span><span style=\"font-family: &quot;&quot;; font-size: 10.5pt; line-height: 1.5; background-color: window;\">&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;</span></span></p>");
		sb.append("<p><span style=\"font-family: &quot;&quot;; line-height: 1.5; background-color: window; font-size: 16px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"font-family: &quot;&quot;; font-size: 10.5pt; line-height: 1.5; background-color: window; color: rgb(255, 0, 0);\">&nbsp;&nbsp;本邮件由系统发出，请勿回复。</span></span></p>");
		sb.append("<p><span style=\"font-size: 16px;\"><span style=\"font-family: &quot;&quot;; font-size: 10.5pt; line-height: 1.5; background-color: window;\">&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;</span><span style=\"background-color: window; font-family: &quot;&quot;; font-size: 10.5pt; line-height: 1.5;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;东南大学|李阳研究院</span></span></p>");
		sb.append("<p><span style=\"font-size: 16px;\"><span microsoft=\"\" yahei=\"\" font-size:=\"\" color:=\"\" background-color:=\"\" font-weight:=\"\" font-style:=\"\" text-decoration:=\"\" style=\"font-size: 16px; font-family: &quot;&quot;;\">&nbsp; &nbsp;&nbsp;</span><span style=\"font-family: &quot;&quot;; font-size: 10.5pt; line-height: 1.5; background-color: window;\">&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+DateUtils.formatDate(new Date(), DateUtils.simple)+"</span></span></p>");
		
		try {
			String res = operation.sendMail(email_username, email_pwd, email_host, email_username, toEmail, title, sb.toString());
			if("success".equals(res)){
				//5分钟验证码自动失效
				removeAttrbute(request.getSession(), ResultCode.MAIL_CODE);
				removeEmail(toEmail);
				return true;
			}
			return false;
		} catch (Exception e) {
			System.out.println("发送邮件异常>>"+e.toString());
		}
		return false;
	}
	
	/**
	 * @Title removeEmail  
	 * @Description 删除邮箱验证码
	 * @author hanpp
	 * @param email void
	 * @date 2019年1月17日 上午11:16:05  
	 * @throws
	 */
	private void removeEmail(final String email) {
		final Timer timer = new Timer();
		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				
				timer.cancel();
			}
		}, 5 * 60 * 1000);
	}
	
	/**
	 * 设置5分钟后删除邮箱验证码
	 * @param session
	 * @param attrName
	 */
	private void removeAttrbute(final HttpSession session, final String key) {
		final Timer timer = new Timer();
		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				//清除redis中邮箱验证码
				session.removeAttribute(key);
				// 删除session中存的验证码
				// session.removeAttribute(key);
				timer.cancel();
			}
		}, 5 * 60 * 1000);
	}
}
